// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   SubmitFilter.java

package include.anti_repeat_submit;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.servlet.*;
import javax.servlet.http.*;

public class SubmitFilter
	implements Filter
{

	private FilterConfig filterConfig;

	public SubmitFilter()
	{
		filterConfig = null;
	}

	public void init(FilterConfig filterConfig)
		throws ServletException
	{
		this.filterConfig = filterConfig;
	}

	public void destroy()
	{
		filterConfig = null;
	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
		throws ServletException, IOException
	{
		HttpServletRequest request;
		HttpSession session;
		HttpServletResponse response;
		String path;
		if (filterConfig == null)
			return;
		request = (HttpServletRequest)servletRequest;
		session = request.getSession();
		response = (HttpServletResponse)servletResponse;
		path = request.getRequestURI();
		path = path.substring(1);
		path = path.split("/")[0];
		String submit;
		String page_submit;
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		submit = (String)session.getAttribute("sumbit_submit");
		page_submit = request.getParameter("sumbit_submit");
		if (page_submit == null)
		{
			filterChain.doFilter(servletRequest, servletResponse);
			return;
		}
		try
		{
			if (submit != null && !submit.equals(page_submit))
			{
				response.sendRedirect((new StringBuilder("/")).append(path).append("/include/error_msg.jsp?finished_tag=0").toString());
			} else
			{
				String token = generateToken(request);
				session.setAttribute("sumbit_submit", token);
				filterChain.doFilter(servletRequest, servletResponse);
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			response.sendRedirect((new StringBuilder("/")).append(path).append("/include/error_msg.jsp?finished_tag=1").toString());
		}
		return;
	}

	protected String generateToken(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		MessageDigest md;
		byte id[] = session.getId().getBytes();
		byte now[] = (new Long(System.currentTimeMillis())).toString().getBytes();
		md = MessageDigest.getInstance("MD5");
		md.update(id);
		md.update(now);
		return toHex(md.digest());
		IllegalStateException e;
		e;
		return null;
		e;
		return null;
	}

	public String toHex(byte buffer[])
	{
		StringBuffer sb = new StringBuffer();
		String s = null;
		for (int i = 0; i < buffer.length; i++)
		{
			s = Integer.toHexString(buffer[i] & 0xff);
			if (s.length() < 2)
				sb.append('0');
			sb.append(s);
		}

		return sb.toString();
	}
}
